package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.common.LifeWayEnum;
import com.gt.app.domain.EsLifeWayInfo;
import com.gt.app.domain.EsLifeWayItem;
import com.gt.app.domain.dto.EsLifeWayInfoDto;
import com.gt.app.mapper.EsLifeWayInfoMapper;
import com.gt.app.service.EsLifeWayInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.service.EsLifeWayItemService;
import com.gt.app.util.CommonUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsLifeWayInfoServiceImpl extends ServiceImpl<EsLifeWayInfoMapper, EsLifeWayInfo> implements EsLifeWayInfoService {

    @Autowired
    private EsLifeWayItemService esLifeWayItemService;

    @Override
    @Transactional
    public EsLifeWayInfoDto add(EsLifeWayInfoDto esLifeWayInfoDto) {
        if (esLifeWayInfoDto == null){
            return null;
        }
        LambdaQueryWrapper<EsLifeWayInfo> eq = new LambdaQueryWrapper<EsLifeWayInfo>().eq(EsLifeWayInfo::getEsCheckId, esLifeWayInfoDto.getEsCheckId());
        EsLifeWayInfo lifeWayInfo = this.getOne(eq);
        if (lifeWayInfo != null){
            this.remove(eq);
            LambdaQueryWrapper<EsLifeWayItem> wrapper = new LambdaQueryWrapper<EsLifeWayItem>()
                    .eq(EsLifeWayItem::getLiveOrdrowid, lifeWayInfo.getOrdrowid());
            esLifeWayItemService.remove(wrapper);
        }

        EsLifeWayInfo esLifeWayInfo = new EsLifeWayInfo();
        BeanUtils.copyProperties(esLifeWayInfoDto, esLifeWayInfo);
        this.save(esLifeWayInfo);

        if (CommonUtils.isNotBlank(esLifeWayInfoDto.getDrinkCases())) {
            ArrayList<EsLifeWayItem> esLifeWayItems = new ArrayList<>();
            for (String item : esLifeWayInfoDto.getDrinkCases()) {
                EsLifeWayItem esLifeWayItem = new EsLifeWayItem();
                esLifeWayItem.setLiveOrdrowid(esLifeWayInfo.getOrdrowid());
                esLifeWayItem.setItem(item);
                esLifeWayItem.setType(LifeWayEnum.DRINK_CASES.getName());
                esLifeWayItems.add(esLifeWayItem);
            }
            esLifeWayItemService.saveBatch(esLifeWayItems);
        }

        if (CommonUtils.isNotBlank(esLifeWayInfoDto.getTeaType())) {
            ArrayList<EsLifeWayItem> esLifeWayItems = new ArrayList<>();
            for (String item : esLifeWayInfoDto.getTeaType()) {
                EsLifeWayItem esLifeWayItem = new EsLifeWayItem();
                esLifeWayItem.setLiveOrdrowid(esLifeWayInfo.getOrdrowid());
                esLifeWayItem.setItem(item);
                esLifeWayItem.setType(LifeWayEnum.TEA_TYPE.getName());
                esLifeWayItems.add(esLifeWayItem);
            }
            esLifeWayItemService.saveBatch(esLifeWayItems);
        }

        if (CommonUtils.isNotBlank(esLifeWayInfoDto.getLiveWaterSource())) {
            ArrayList<EsLifeWayItem> esLifeWayItems = new ArrayList<>();
            for (String item : esLifeWayInfoDto.getLiveWaterSource()) {
                EsLifeWayItem esLifeWayItem = new EsLifeWayItem();
                esLifeWayItem.setLiveOrdrowid(esLifeWayInfo.getOrdrowid());
                esLifeWayItem.setItem(item);
                esLifeWayItem.setType(LifeWayEnum.LIVE_WATER_SOURCE.getName());
                esLifeWayItems.add(esLifeWayItem);
            }
            esLifeWayItemService.saveBatch(esLifeWayItems);
        }

        if (CommonUtils.isNotBlank(esLifeWayInfoDto.getDrinkWaterSource())) {
            ArrayList<EsLifeWayItem> esLifeWayItems = new ArrayList<>();
            for (String item : esLifeWayInfoDto.getDrinkWaterSource()) {
                EsLifeWayItem esLifeWayItem = new EsLifeWayItem();
                esLifeWayItem.setLiveOrdrowid(esLifeWayInfo.getOrdrowid());
                esLifeWayItem.setItem(item);
                esLifeWayItem.setType(LifeWayEnum.DRINK_WATER_SOURCE.getName());
                esLifeWayItems.add(esLifeWayItem);
            }
            esLifeWayItemService.saveBatch(esLifeWayItems);
        }

        if (CommonUtils.isNotBlank(esLifeWayInfoDto.getPhysicalWay())) {
            ArrayList<EsLifeWayItem> esLifeWayItems = new ArrayList<>();
            for (String item : esLifeWayInfoDto.getPhysicalWay()) {
                EsLifeWayItem esLifeWayItem = new EsLifeWayItem();
                esLifeWayItem.setLiveOrdrowid(esLifeWayInfo.getOrdrowid());
                esLifeWayItem.setItem(item);
                esLifeWayItem.setType(LifeWayEnum.PHYSICAL_WAY.getName());
                esLifeWayItems.add(esLifeWayItem);
            }
            esLifeWayItemService.saveBatch(esLifeWayItems);
        }
        return esLifeWayInfoDto;
    }

    @Override
    public EsLifeWayInfoDto getLifeWayInfo(String checkId) {
        EsLifeWayInfoDto esLifeWayInfoDto = new EsLifeWayInfoDto();

        LambdaQueryWrapper<EsLifeWayInfo> eq = new LambdaQueryWrapper<EsLifeWayInfo>().eq(EsLifeWayInfo::getEsCheckId, checkId);
        EsLifeWayInfo esLifeWayInfo = this.getOne(eq);
        if (esLifeWayInfo == null){
            return null;
        }
        BeanUtils.copyProperties(esLifeWayInfo, esLifeWayInfoDto);

        LambdaQueryWrapper<EsLifeWayItem> eq1 = new LambdaQueryWrapper<EsLifeWayItem>().eq(EsLifeWayItem::getLiveOrdrowid, esLifeWayInfo.getOrdrowid());
        List<EsLifeWayItem> list = esLifeWayItemService.list(eq1);

        List<String> drinkCases = new ArrayList<>();
        List<String> teaType = new ArrayList<>();
        List<String> liveWaterSource = new ArrayList<>();
        List<String> drinkWaterSource = new ArrayList<>();
        List<String> physicalWay = new ArrayList<>();
        for (EsLifeWayItem esLifeWayItem : list) {
            if (LifeWayEnum.DRINK_CASES.getName().equals(esLifeWayItem.getType())){
                drinkCases.add(esLifeWayItem.getItem());
            }
            if (LifeWayEnum.TEA_TYPE.getName().equals(esLifeWayItem.getType())){
                teaType.add(esLifeWayItem.getItem());
            }
            if (LifeWayEnum.LIVE_WATER_SOURCE.getName().equals(esLifeWayItem.getType())){
                liveWaterSource.add(esLifeWayItem.getItem());
            }
            if (LifeWayEnum.DRINK_WATER_SOURCE.getName().equals(esLifeWayItem.getType())){
                drinkWaterSource.add(esLifeWayItem.getItem());
            }
            if (LifeWayEnum.PHYSICAL_WAY.getName().equals(esLifeWayItem.getType())){
                physicalWay.add(esLifeWayItem.getItem());
            }
        }
        esLifeWayInfoDto.setDrinkCases(drinkCases);
        esLifeWayInfoDto.setTeaType(teaType);
        esLifeWayInfoDto.setLiveWaterSource(liveWaterSource);
        esLifeWayInfoDto.setDrinkWaterSource(drinkWaterSource);
        return esLifeWayInfoDto;
    }
}
